//
//  KooCalendarConfig.m
//  KooIEDUCalendarDemo
//
//  Created by nowcoder on 2024.
//

#import "KooCalendarConfig.h"

@implementation KooCalendarConfig

#pragma mark - Initialization

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupDefaultConfig];
    }
    return self;
}

#pragma mark - Private Methods

- (void)setupDefaultConfig {
    // 基础配置
    self.maxSelectCount = 10;
    self.onlyFutureDates = YES;
    self.showTodayMark = YES;
    
    // 尺寸配置
    self.cellSize = CGSizeMake(40, 70);
    self.cellSpacing = CGSizeMake(1, 1);
    self.weekViewHeight = 32;
    self.headerHeight = 44;
    
    // 颜色配置
    self.backgroundColor = [UIColor whiteColor];
    self.todayBackgroundColor = [UIColor systemBlueColor];
    self.todayTextColor = [UIColor whiteColor];
    self.selectedBackgroundColor = [UIColor systemOrangeColor];
    self.selectedTextColor = [UIColor whiteColor];
    self.normalTextColor = [UIColor blackColor];
    self.disabledTextColor = [UIColor lightGrayColor];
    self.weekTitleColor = [UIColor darkGrayColor];
    self.monthTitleColor = [UIColor blackColor];
    
    // 字体配置
    self.dateFont = [UIFont systemFontOfSize:16];
    self.weekTitleFont = [UIFont systemFontOfSize:14];
    self.monthTitleFont = [UIFont boldSystemFontOfSize:18];
    
    // 文案配置
    self.weekTitles = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    self.monthFormat = @"yyyy年MM月";
    self.todayMarkText = @"今天";
    
    // 圆角配置
    self.cellCornerRadius = self.cellSize.width / 2.0; // 默认圆形
    self.enableCornerRadius = YES;
    
    // 动画配置
    self.selectAnimationDuration = 0.2;
    self.monthSwitchAnimationDuration = 0.3;
}

#pragma mark - Factory Methods

+ (instancetype)defaultConfig {
    return [[self alloc] init];
}

+ (instancetype)minimalistConfig {
    KooCalendarConfig *config = [[self alloc] init];
    
    // 简约风格配置
    config.cellSize = CGSizeMake(35, 35);
    config.cellSpacing = CGSizeMake(2, 2);
    config.enableCornerRadius = NO;
    config.cellCornerRadius = 0;
    
    // 简约颜色
    config.todayBackgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
    config.selectedBackgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    config.normalTextColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    config.weekTitleColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
    
    // 简约字体
    config.dateFont = [UIFont systemFontOfSize:15];
    config.weekTitleFont = [UIFont systemFontOfSize:13];
    config.monthTitleFont = [UIFont systemFontOfSize:17];
    
    return config;
}

+ (instancetype)darkThemeConfig {
    KooCalendarConfig *config = [[self alloc] init];
    
    // 深色主题配置
    config.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1.0];
    config.todayBackgroundColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0];
    config.selectedBackgroundColor = [UIColor colorWithRed:1.0 green:0.6 blue:0.0 alpha:1.0];
    config.normalTextColor = [UIColor whiteColor];
    config.disabledTextColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
    config.weekTitleColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
    config.monthTitleColor = [UIColor whiteColor];
    
    return config;
}

+ (instancetype)imageBasedConfig {
    KooCalendarConfig *config = [[self alloc] init];
    
    // 基于图片设计的配置
    config.maxSelectCount = 10;
    config.onlyFutureDates = YES;
    config.showTodayMark = YES;
    
    // 尺寸配置 - 基于图片中的比例
    config.cellSize = CGSizeMake(42, 42);
    config.cellSpacing = CGSizeMake(2, 8);
    config.weekViewHeight = 32;
    config.headerHeight = 44;
    
    // 颜色配置 - 基于图片设计
    config.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.0];
    config.todayBackgroundColor = [UIColor clearColor]; // 今天不用背景色，只用文字
    config.todayTextColor = [UIColor blackColor];
    config.selectedBackgroundColor = [UIColor clearColor]; // 选中用边框，不用背景
    config.selectedTextColor = [UIColor colorWithRed:0.0 green:0.7 blue:0.4 alpha:1.0]; // 绿色
    config.normalTextColor = [UIColor blackColor];
    config.disabledTextColor = [UIColor lightGrayColor];
    config.weekTitleColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
    config.monthTitleColor = [UIColor blackColor];
    
    // 字体配置 - 基于图片设计
    config.dateFont = [UIFont systemFontOfSize:16];
    config.weekTitleFont = [UIFont systemFontOfSize:14];
    config.monthTitleFont = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
    
    // 文案配置
    config.weekTitles = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    config.monthFormat = @"yyyy年MM月";
    config.todayMarkText = @"今";
    
    // 圆角配置 - 基于图片中的方形设计
    config.cellCornerRadius = 6; // 轻微圆角
    config.enableCornerRadius = YES;
    
    // 动画配置
    config.selectAnimationDuration = 0.15;
    config.monthSwitchAnimationDuration = 0.25;
    
    return config;
}

#pragma mark - Validation Methods

- (BOOL)isValid {
    // 检查基础配置
    if (self.maxSelectCount <= 0) return NO;
    
    // 检查尺寸配置
    if (self.cellSize.width <= 0 || self.cellSize.height <= 0) return NO;
    if (self.weekViewHeight <= 0 || self.headerHeight <= 0) return NO;
    
    // 检查颜色配置
    if (!self.backgroundColor || !self.todayBackgroundColor || 
        !self.selectedBackgroundColor || !self.normalTextColor) return NO;
    
    // 检查字体配置
    if (!self.dateFont || !self.weekTitleFont || !self.monthTitleFont) return NO;
    
    // 检查文案配置
    if (!self.weekTitles || self.weekTitles.count != 7) return NO;
    if (!self.monthFormat || self.monthFormat.length == 0) return NO;
    
    // 检查动画配置
    if (self.selectAnimationDuration < 0 || self.monthSwitchAnimationDuration < 0) return NO;
    
    return YES;
}

- (void)fixInvalidConfigs {
    KooCalendarConfig *defaultConfig = [KooCalendarConfig defaultConfig];
    
    // 修正基础配置
    if (self.maxSelectCount <= 0) {
        self.maxSelectCount = defaultConfig.maxSelectCount;
    }
    
    // 修正尺寸配置
    if (self.cellSize.width <= 0 || self.cellSize.height <= 0) {
        self.cellSize = defaultConfig.cellSize;
    }
    if (self.weekViewHeight <= 0) {
        self.weekViewHeight = defaultConfig.weekViewHeight;
    }
    if (self.headerHeight <= 0) {
        self.headerHeight = defaultConfig.headerHeight;
    }
    
    // 修正颜色配置
    if (!self.backgroundColor) {
        self.backgroundColor = defaultConfig.backgroundColor;
    }
    if (!self.todayBackgroundColor) {
        self.todayBackgroundColor = defaultConfig.todayBackgroundColor;
    }
    if (!self.selectedBackgroundColor) {
        self.selectedBackgroundColor = defaultConfig.selectedBackgroundColor;
    }
    if (!self.normalTextColor) {
        self.normalTextColor = defaultConfig.normalTextColor;
    }
    if (!self.disabledTextColor) {
        self.disabledTextColor = defaultConfig.disabledTextColor;
    }
    if (!self.weekTitleColor) {
        self.weekTitleColor = defaultConfig.weekTitleColor;
    }
    if (!self.monthTitleColor) {
        self.monthTitleColor = defaultConfig.monthTitleColor;
    }
    
    // 修正字体配置
    if (!self.dateFont) {
        self.dateFont = defaultConfig.dateFont;
    }
    if (!self.weekTitleFont) {
        self.weekTitleFont = defaultConfig.weekTitleFont;
    }
    if (!self.monthTitleFont) {
        self.monthTitleFont = defaultConfig.monthTitleFont;
    }
    
    // 修正文案配置
    if (!self.weekTitles || self.weekTitles.count != 7) {
        self.weekTitles = defaultConfig.weekTitles;
    }
    if (!self.monthFormat || self.monthFormat.length == 0) {
        self.monthFormat = defaultConfig.monthFormat;
    }
    if (!self.todayMarkText) {
        self.todayMarkText = defaultConfig.todayMarkText;
    }
    
    // 修正圆角配置
    if (self.cellCornerRadius < 0) {
        self.cellCornerRadius = defaultConfig.cellCornerRadius;
    }
    
    // 修正动画配置
    if (self.selectAnimationDuration < 0) {
        self.selectAnimationDuration = defaultConfig.selectAnimationDuration;
    }
    if (self.monthSwitchAnimationDuration < 0) {
        self.monthSwitchAnimationDuration = defaultConfig.monthSwitchAnimationDuration;
    }
}

#pragma mark - Setters

- (void)setCellSize:(CGSize)cellSize {
    _cellSize = cellSize;
    // 当单元格尺寸改变时，自动更新圆角半径
    if (self.enableCornerRadius) {
        self.cellCornerRadius = cellSize.width / 2.0;
    }
}

@end 
